﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class _Default : BasicPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BasicPageInit();
            if (!IsPostBack)
            {
                FillData();
            }
        }

        //填充数据库
        private void FillData()
        {
            var sql = "select * from Task";
            var dt = DbHelper.GetData(sql);
            GridTask.DataSource = dt;
            GridTask.DataBind();
            
        }

        //获取数据库行与列的值
        private string GetValue(int rowIndex,int colIndex)
        {
            var control = GridTask.Rows[rowIndex].Cells[colIndex];
            var ret = control.Controls.Count > 0 ? ((TextBox)control.Controls[0]).Text : control.Text;
            return ret;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("TaskEdit.aspx");
        }

        protected void GridTask_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetValue(e.RowIndex, 0);
            var sql = string.Format("delete from task where id='{0}'", id);
            DbHelper.ExeOperation(sql);
            FillData();

        }

        protected void GridTask_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Response.Redirect("TaskEdit.aspx?id="+GetValue(e.NewEditIndex,0));
        }

        protected void GridTask_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridTask.PageIndex = e.NewPageIndex;
            FillData();
        }

        protected void GridTask_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if(e.CommandName == "detial")
            {
                var index = Convert.ToInt32(e.CommandArgument);//获取命令的参数
                var id = Convert.ToInt32(GetValue(index,0));
                Response.Redirect("TaskDetial.aspx?id=" + id);
            }


        }
    }
}